$(function () {
    BootstrapTableUtil.initTableDataByServer("/bflow/act/list", function () {
        //渲染 复选框
        $(".bootstrap-switch").each(function () {
            $this = $(this), data_on_text = $this.data("on-text") || "", data_off_text = $this.data("off-text") || "";
            $this.bootstrapSwitch({
                onText: data_on_text,
                offText: data_off_text,
                onSwitchChange: function () {
                    var url = $(this).data("switch-url");
                    if (CommUtil.isEmpty(url)) {
                        return;
                    }
                    //异步更新
                    var flag = $(this).is(":checked");
                    HttpUtil.ajaxAsyncJsonPost(url, {flag: flag}, function (message) {
                        dialogTip(message);
                        refreshPage(1500);
                    });
                }
            });
        });
    });
});

function formatterStatus(value, row, index) {
    var checked = "1"==value?"checked":"";
    var str;
    //可操作 非默认的 并且 无效状态的
    if(!row.key.startWith("def_")){
        str = '<input name="status-'+index+'" class="bootstrap-switch" data-switch-url="/bflow/act/editStatus?id='+row.id+'" type="checkbox" '+checked+' value="'+value+'" data-toggle="switch" data-on-text="有效" data-off-text="无效"/>';
    }else{
        var css = "1"==value?"success":"default";
        var text = "1"==value?"有效":"无效";
        str = '<label class="label label-'+css+'">'+text+'</label>';
    }
    return str;
}

function formatterKey(value, row, index) {
    var tag = "";
    if(value.startWith("def_")){
        tag = "&nbsp;<label class='label label-danger'>默认</label>";
    }
    return value+tag;
}

function formatterAssigneeType(value, row, index) {
    var text;
    if("ASSIGN_USER_ID" == value){
        text = "手动分派用户";
    }else if("ASSIGN_ROLE_KEY" == value){
        text = "手动分派角色";
    }else if("USER_ID" == value){
        text = "指定用户";
    }else if("ROLE_KEY" == value){
        text = "指定角色";
    }
    return text;
}


function formatterOperation(value, row, index) {
    var buts ='<a href="/static/bflow/ui/define/actView.html?id='+row.id+'" class="btn btn-info btn-xs">详情</a>';
    //可操作 非默认的 并且 无效状态的
    if(!row.key.startWith("def_")){
        buts += '<a href="/static/bflow/ui/define/actForm.html?id='+row.id+'" class="btn btn-primary btn-xs mgl-5">编辑</a>';
        if(row.status == "0"){//无效的 才可以删除
            buts += '<a href="javascript:del(\''+row.id+'\', \''+row.name+'\')" class="btn btn-danger btn-xs mgl-5">删除</a>';
        }
    }
    return buts;
}


function del(id, name) {
    dialogConfirm(name, function(){
        HttpUtil.ajaxAsyncJsonPost("/bflow/act/del", {id: id}, function(message){
            dialogTipSuccess(message);
            BootstrapTableUtil.refresh(actTableId);
        });
    }, "确定删除记录？");
}